import EditMng from '../paint_plugin/edit-mng'
import {EN,Action} from '../config'
const BaseEdit = {
	init(){
		this.editMng = new EditMng(this.$event)
		this.editMng.on(EN.action,(data)=>{
			if(!this.active)return
			if(!this.paths)return
			this[data.action]&&this[data.action](data)
		})
		this.flag = 0 //click 1 drag
	},
	updateFlag(v){
		this.flag=v
	},
	onRemoveAction(){
		let rs = this.paths
		this.editMng.fadeOut()
		this.$Confirm.focusIn({
			text:`确认移除该条条路径`
		}).done(()=>{
			this.paths=[]
			this.$version.remove(rs.map(r=>r.id))
			this.editMng.focusOut()
			this.ghostCtx.pathMode = false
			this.clearRect()
		}).fail(()=>{
			this.editMng.fadeIn()
		})
	},
	onEditAction(data){
		this.clearRect()
		let {k,v,idx} = data
		let path = this.paths[0]
		if(data.action==="onStepAction")
			path.step[idx][k] = v
		if(data.action==="onStateAction")
			path.state[k] = v
		if(idx!==-1&&path.action!==Action.Point)
			this.hpoint(path.step[idx])
		else this.hpath(path)
		this.$version.updateMulti(this.paths)
	},
	onStepAction(data){
		this.onEditAction(data)
	},
	onStateAction(data){
		this.onEditAction(data)
	},
	onClick(pos,e,meta){
		this.clearRect()
		let rs = this.$version.queryInPath(pos)
		this.emit(EN.pathFocus,rs.map(r=>r.id))
		if(rs.isEmpty()){
			this.ghostCtx.pathMode = false
			return
		}
		this.ghostCtx.pathMode = true
		this.paths = rs

		let idx = rs[0].step.findIndex(p=>Math.distance(p,pos)<p.r)
		if(this.flag===0&&rs.length===1){
			let {x,y} = e.epos()
			this.editMng.focusIn({
				left:x,
				top:y
			},rs[0],idx)
		}
		if(rs.length===1&&idx!==-1)
			this.hpoint(rs[0].step[0])
		else
		rs.forEach(s=>{
			this.hpath(s)
		})
	},
	onPathFocusByID(id){
		let r = this.$version.queryById(id)
		if(!r)return
		this.clearRect()
		this.hpath(r)
		this.ghostCtx.pathMode = true
	}
}
export default BaseEdit